home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 3 of 3.iso
/
chapte21
/
avicopy.c
next >
Wrap
C/C++ Source or Header
|
1996-04-29
|
17KB
|
597 lines
#include <windows.h>
#include <stdio.h>
#include "AviCopy.h"
#include <vfw.h>
#if defined (WIN32)
#define IS_WIN32 TRUE
#else
#define IS_WIN32 FALSE
#endif
#define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "My Application";
// the rest of the stuff
//......................
BOOL RegisterWin95( CONST WNDCLASS* lpwc );
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( IS_WIN95 )
{
if ( !RegisterWin95( &wc ) )
return( FALSE );
}
else if ( !RegisterClass( &wc ) )
return( FALSE );
hInst = hInstance;
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
WNDCLASSEX wcex;
wcex.style = lpwc->style;
wcex.lpfnWndProc = lpwc->lpfnWndProc;
wcex.cbClsExtra = lpwc->cbClsExtra;
wcex.cbWndExtra = lpwc->cbWndExtra;
wcex.hInstance = lpwc->hInstance;
wcex.hIcon = lpwc->hIcon;
wcex.hCursor = lpwc->hCursor;
wcex.hbrBackground = lpwc->hbrBackground;
wcex.lpszMenuName = lpwc->lpszMenuName;
wcex.lpszClassName = lpwc->lpszClassName;
// Added elements for Windows 95.
//...............................
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
return RegisterClassEx( &wcex );
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}
#define MSG_LEN 1024
char msg[MSG_LEN+1];
int nTabStop = 100;
HWND hListBox = NULL;
VOID CopyAVI(HWND hWnd, UINT uOption);
VOID CopyStream(HWND hWnd, PAVISTREAM pAviStream, AVISTREAMINFO* pStreamInfo);
VOID DisplayAviInfo(AVIFILEINFO* pFileInfo);
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_CREATE :
// initialize the AVI library DLL
//...............................
AVIFileInit();
// Create ListBox
//...............
hListBox = CreateWindow( "LISTBOX", "",
WS_CHILD | LBS_NOTIFY |
WS_VSCROLL | WS_BORDER |
WS_VISIBLE | LBS_NOINTEGRALHEIGHT |
LBS_USETABSTOPS,
0, 0,
0, 0,
hWnd,
(HMENU)101,
hInst,
NULL );
SendMessage(hListBox, LB_SETTABSTOPS, 1, (LPARAM)&nTabStop);
break;
case WM_SIZE :
MoveWindow( hListBox, 0, 0,
LOWORD( lParam ),
HIWORD( lParam ), TRUE );
break;
case WM_COMMAND :
switch( LOWORD(wParam) )
{
case IDM_COPYVIDEO:
case IDM_COPYALL :
{
SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
CopyAVI(hWnd, LOWORD(wParam) );
}
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
// release the AVI library DLL
//............................
AVIFileExit();
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return(0L);
}
/*****************************************************************************
* Global variables
*
*
*
******************************************************************************/
HRESULT rc;
LPCTSTR lpszInputFilename = "small.avi";
PAVIFILE pAviIn = NULL;
PAVIFILE pAviOut = NULL;
PAVISTREAM pAviTmpStream = NULL;
LONG nAviStreams = 0L; // number of pAviStreams[];
PAVISTREAM pAviStream[2] = { NULL, NULL };
AVICOMPRESSOPTIONS Options[2];
LPAVICOMPRESSOPTIONS pOptions[2];
AVIFILEINFO pFileInfo;
AVISTREAMINFO pStreamInfo;
/*****************************************************************************
* CopyAVI
*
*
*
******************************************************************************/
VOID CopyAVI(HWND hWnd, UINT uOption)
{
LONG i;
// open input AVI file
//....................
rc = AVIFileOpen(&pAviIn, lpszInputFilename, OF_READ, NULL);
if (rc) // if error
{
MessageBox(hWnd, "Error opening AVI file.",
NULL, MB_OK);
return;
}
// open output file
//.................
remove("AviCopy.avi"); // destroy previous version (if any)
rc = AVIFileOpen(&pAviOut, "AviCopy.avi", OF_CREATE|OF_WRITE, NULL);
if (rc)
{
MessageBox(hWnd, "Error creating new AVI file.",
NULL, MB_OK);
return;
}
// get input AVI file info
//........................
rc = AVIFileInfo(pAviIn, &pFileInfo, sizeof(AVIFILEINFO));
DisplayAviInfo(&pFileInfo);
UpdateWindow(hWnd);
// write optional header data
//...........................
sprintf(msg, "Copy of %s. Created by AVICOPY.", lpszInputFilename);
AVIFileWriteData(pAviOut, mmioStringToFOURCC("Copy", 0),
(LPVOID)msg, strlen(msg)+1);
// get AVI streams
//................
if (uOption == IDM_COPYVIDEO) // copy only video
{
rc = AVIStreamOpenFromFile(&pAviTmpStream, lpszInputFilename,
streamtypeVIDEO, 0, OF_READ, NULL);
if (!rc)
pAviStream[nAviStreams++] = pAviTmpStream;
}
else
{
for (i = 0; i < (LONG)pFileInfo.dwStreams; i++)
{
rc = AVIFileGetStream(pAviIn, &pAviTmpStream, 0L, i);
if (rc)
continue;
rc = AVIStreamInfo(pAviTmpStream, &pStreamInfo, sizeof(AVISTREAMINFO));
if (rc)
continue;
if (pStreamInfo.fccType == streamtypeVIDEO)
pAviStream[nAviStreams++] = pAviTmpStream;
else if (pStreamInfo.fccType == streamtypeAUDIO &&
uOption == IDM_COPYALL )
pAviStream[nAviStreams++] = pAviTmpStream;
else
AVIStreamRelease(pAviTmpStream);
}
}
// get save (compression) options
//...............................
for (i = 0; i < nAviStreams; i++)
{
// AVISaveOptions takes an array of pointers to our
// compression options (cleared)
pOptions[i] = &Options[i];
memset(pOptions[i], 0, sizeof(AVICOMPRESSOPTIONS));
}
AVISaveOptions(hWnd,
ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE |
ICMF_CHOOSE_PREVIEW,
nAviStreams,
pAviStream,
pOptions);
// copy streams
//.............
for (i = 0; i < nAviStreams; i++)
{
pAviTmpStream = NULL;
rc = AVIMakeCompressedStream(&pAviTmpStream,
pAviStream[i],
pOptions[i],
NULL);
// check if it can be compressed
// this can happen if no compression was requested
//................................................
if (rc == AVIERR_OK)
{ // use compressed stream
AVIStreamInfo(pAviTmpStream, &pStreamInfo, sizeof(AVISTREAMINFO));
CopyStream(hWnd, pAviTmpStream, &pStreamInfo);
if (pAviTmpStream)
AVIStreamRelease(pAviTmpStream);
}
else
{ // use original stream
AVIStreamInfo(pAviStream[i], &pStreamInfo, sizeof(AVISTREAMINFO));
CopyStream(hWnd, pAviStream[i], &pStreamInfo);
}
}
AVISaveOptionsFree(nAviStreams, pOptions);
// close AVI files
//................
AVIFileRelease(pAviIn);
AVIFileRelease(pAviOut);
}
/*****************************************************************************
* CopyStream
*
*
*
******************************************************************************/
VOID CopyStream(HWND hWnd, PAVISTREAM pAviStream, AVISTREAMINFO* pStreamInfo)
{
LONG nFormatSize;
LONG nSampleSize;
LONG nStreamSize;
LONG nSamplePosition;
LONG nSamplesRead;
PAVISTREAM pNewStream;
LPVOID pBuffer = NULL;
LONG nBufferSize = 32767L;
// alloc buffer
//.............
pBuffer = HeapAlloc( GetProcessHeap(),
HEAP_ZERO_MEMORY,
nBufferSize);
// read stream format
//...................
rc = AVIStreamFormatSize(pAviStream, 0, &nFormatSize);
if (!rc && nFormatSize > nBufferSize)
{
pBuffer = HeapReAlloc( GetProcessHeap(),
HEAP_ZERO_MEMORY,
pBuffer,
nFormatSize);
if (pBuffer)
nBufferSize = nFormatSize;
}
rc = AVIStreamReadFormat(pAviStream, 0, pBuffer, &nFormatSize);
// create new video stream
//........................
rc = AVIFileCreateStream(pAviOut, &pNewStream, pStreamInfo);
if (rc)
{
MessageBox(NULL, "Error creating new video stream.", NULL, MB_OK);
return;
}
// Set format of new stream
//.........................
rc = AVIStreamSetFormat(pNewStream, 0, pBuffer, nFormatSize);
if (rc)
{
MessageBox(NULL, "Error setting stream format.", NULL, MB_OK);
AVIStreamRelease(pNewStream);
return;
}
// allocate buffer, instruct AVIStreamRead to
// suggest buffer size to use
//...........................................
rc = AVIStreamRead(pAviStream,
AVIStreamStart(pAviStream),
AVISTREAMREAD_CONVENIENT,
NULL, 0L, &nStreamSize, NULL);
// check if we must enlarge the buffer
if (!rc && nStreamSize > nBufferSize)
{
pBuffer = HeapReAlloc( GetProcessHeap(),
HEAP_ZERO_MEMORY,
pBuffer,
nStreamSize);
if (pBuffer)
nBufferSize = nStreamSize;
}
// Read the stream data
//.....................
for (nSamplePosition = AVIStreamStart(pAviStream);
pBuffer && nSamplePosition < AVIStreamEnd(pAviStream); )
{
AVIStreamSampleSize(pAviStream, nSamplePosition, &nSampleSize);
rc = AVIStreamRead(pAviStream, nSamplePosition,
(nBufferSize / nSampleSize), // est. sample to read
pBuffer, nBufferSize,
&nStreamSize, &nSamplesRead);
rc = AVIStreamWrite(pNewStream, nSamplePosition, nSamplesRead,
pBuffer, nStreamSize,
AVIIF_KEYFRAME, &nSamplesRead, &nStreamSize);
nSamplePosition += nSamplesRead;
// display status
sprintf(msg, "Copying stream: <%s>, %ld of %ld samples written.",
pStreamInfo->szName,
nSamplePosition, AVIStreamEnd(pAviStream));
SetWindowText(hWnd, msg);
}
HeapFree(GetProcessHeap(), 0, pBuffer);
// Close the stream and file
//..........................
AVIStreamRelease(pNewStream);
SetWindowText(hWnd, lpszTitle);
}
/*****************************************************************************
* DisplayAviInfo
*
*
*
******************************************************************************/
#define Display(msg) SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)msg)
VOID DisplayAviInfo(AVIFILEINFO* pFileInfo)
{
SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
sprintf(msg, "Max bytes/sec:\t %ld", pFileInfo->dwMaxBytesPerSec);
Display(msg);
sprintf(msg, "Flags (has index):\t %s",
(pFileInfo->dwFlags & AVIFILEINFO_HASINDEX ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Flags (must use index):\t %s",
(pFileInfo->dwFlags & AVIFILEINFO_MUSTUSEINDEX ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Flags (is interleaved):\t %s",
(pFileInfo->dwFlags & AVIFILEINFO_ISINTERLEAVED ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Flags (was capture file):\t %s",
(pFileInfo->dwFlags & AVIFILEINFO_WASCAPTUREFILE ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Flags (copyrighted):\t %s",
(pFileInfo->dwFlags & AVIFILEINFO_COPYRIGHTED ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Caps (can read):\t %s",
(pFileInfo->dwCaps & AVIFILECAPS_CANREAD ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Caps (can write):\t %s",
(pFileInfo->dwCaps & AVIFILECAPS_CANWRITE ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Caps (all key frames):\t %s",
(pFileInfo->dwCaps & AVIFILECAPS_ALLKEYFRAMES ? "Yes" : "No") );
Display(msg);
sprintf(msg, "Caps (compressed):\t %s", // read carefully (it's backwards)
(pFileInfo->dwCaps & AVIFILECAPS_NOCOMPRESSION ? "No" : "Yes") );
Display(msg);
sprintf(msg, "Number of streams:\t %ld", pFileInfo->dwStreams);
Display(msg);
sprintf(msg, "Sugg. buffer size:\t %ld", pFileInfo->dwSuggestedBufferSize);
Display(msg);
sprintf(msg, "Width:\t %ld", pFileInfo->dwWidth);
Display(msg);
sprintf(msg, "Height:\t %ld", pFileInfo->dwHeight);
Display(msg);
sprintf(msg, "Scale:\t %ld", pFileInfo->dwScale);
Display(msg);
sprintf(msg, "Rate:\t %ld", pFileInfo->dwRate);
Display(msg);
sprintf(msg, "Length:\t %ld", pFileInfo->dwLength);
Display(msg);
sprintf(msg, "Edit count:\t %ld", pFileInfo->dwEditCount);
Display(msg);
sprintf(msg, "File type:\t %s", pFileInfo->szFileType);
Display(msg);
}